package fnic.prehand.esagent.passitive_measure;

import org.apache.log4j.Logger;

import fnic.prehand.augment.AppInfo;
import fnic.prehand.augment.Flow;
import fnic.prehand.augment.IPConverter;
import fnic.prehand.augment.IpInfo;
import fnic.prehand.augment.UserInfo;
import fnic.prehand.augment.resource.AugmentResource;

/**
 * 未用分布式处理的扩维模块，flow_ipv4的流数据;其算法还是用以前预处理的（各种copy）
 * @author Wu
 *
 */
public class MakeAugment{
	private static final int flowize = 19;
	private static final int augmentsize = 12;
	//依赖的资源库
	private AugmentResource resource;
    //各种参数
	private long[] fields ;
	private long searchedIp;
	private long ispId;
	private long districtId;
	private UserInfo user;
	private AppInfo app;
	private int result;
	private UserInfo otherInnerUser;
	private AppInfo undefindApp;
	private IpInfo ip;
	
	public MakeAugment() {
		resource = AugmentResource.getResource();
	}

	public void augment(Flow f) {
//		String[] flowfields = lineStr.split(", ");
//		if(flowfields.length!=flowize) // 应该不会出现;但还是保留
//		{   
//			System.out.println("flow file data format error:"+lineStr);
//			return null;
//		}
		
//		Flow f =new Flow();
//		f.num_oldFlow = new long[flowize];
//		fields = f.num_oldFlow;
//		int n=0;
//		for(String field : flowfields){
//			fields[n++] = Long.parseLong(field);
//		}
	
		fields = f.num_oldFlow;
		otherInnerUser = new UserInfo();
		otherInnerUser.userID=0;
		otherInnerUser.userGroupID=0;
		undefindApp = new AppInfo();
		undefindApp.appGroupID=-1;
		undefindApp.appID=-1;
		
		searchedIp =  fields[3];
		try{
			result=resource.search(searchedIp);
		}catch(Exception e){
			result=0;
		}
		ip = resource.getIpDB().get(result);
		districtId = ip.getDistrictID();
		ispId = ip.getIspID();
		user = resource.getUserInfoDB().get(fields[2]);
		if(user==null){
			user=otherInnerUser;
			user.userID = IPConverter.ipv42id(fields[2]);
		}
		app = resource.getAppInfoDB().get(fields[13]);
		if(app==null)
			app = undefindApp;
		//将汇聚完的数据封装至Flow中
		f.augFlow = new String[]{app.protocolName,String.valueOf(app.appGroupID),app.appGroupName,
				                                     String.valueOf(user.userGroupID),user.userGroupName,String.valueOf(user.userID),user.userName,
				                                     String.valueOf(districtId),ip.getDistrictName(),String.valueOf(ispId),ip.getIspName(),String.valueOf(0)};
	}
	
	public static int getFlowFieldNum(){
		return flowize;
	}
	
	public static int getAugmentFieldNum(){
		return augmentsize;
	}
	//1399190674, 0, 16777216, 16777216, 51795, 80, 6, 22, 18, 20210, 6203, 0, 0, 1000, 0, 1399190653899, 1399190654391, 0, 0,-1,0,829889836,0,0,0
	public static void main(String[] args) {
		MakeAugment a = new MakeAugment();
		String str = "1399190674, 0, 19136512, 19136512, 51795, 80, 6, 22, 18, 20210, 6203, 0, 0, 1000, 0, 1399190653899, 1399190654391, 0, 0";
		long sTime = System.currentTimeMillis();
		for(int i = 0; i < 1000000 ; i++){ 
//			System.out.println(a.augment(str));
//			a.augment(str);
		}
//		Flow f = (Flow) a.augment(str);
		System.out.println(System.currentTimeMillis() - sTime);
	}
   
}
